﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading.Tasks;
using Microsoft.Data.Sqlite;
using SimpleTool.SerialTool.SerialControl;
using static SimpleTool.Server.PowerPlantManeger;
using System.Runtime.InteropServices;
using Supabase.Realtime.Interfaces;
using Supabase.Realtime.PostgresChanges;
using Supabase.Postgrest.Models;
using SimpleTool.ActivityUnit;
using static SimpleTool.SerialTool.SerialControl.SerialViewModel;

namespace SimpleTool.Server
{
    public interface IPowerPlant
    {
        public SerialPort GetSerialPortiInstance();

        /// <summary>
        /// 0-Leave 1-Enter //
        /// EventType = 1  // 设备配置已更改
        /// EventType = 2  // 设备已到达(插入)
        /// EventType = 3  // 设备已移除
        /// EventType = 4  // 设备即将删除(允许阻止)
        /// </summary>
        public void Moniter(Action Out, int type = 1,  bool isOpen = false);

        /// <summary>
        /// 0-close 1-open 2-isOpen
        /// </summary>
        /// <param name="stat"></param>
        public (bool, string) Action(int stat = 1);

        public void InvokeCallBack(object sender, System.Management.EventArrivedEventArgs e);

        public void BuffClear();

        public void Subscribe(bool isSubscribe = true);

        public void Write(List<CommandSectionModel.CommandStruct> pushs);

        public bool Execute(string order);

        public void Read<T>(ReceiveBuffContent action);

    }

    public interface ISerialServer
    {
        public ObservableCollection<string> ReceiveSerialInfo(EmSerialInfoType type);

    }

    public interface IDatabaseServer
    {

        public bool GetIdentify();

        public void InitLocalDB(SqliteConnectionConfig Configurate);

        public Task<bool> InitCloudDB(string address, string key);

        public Supabase.Client? GetCloudDB();

        /// <summary>
        /// 0 - local, 1 - supabase
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public KeyValuePair<string, string> GetImportantInfo(bool cloud = true);

        public void SetImportantInfo(KeyValuePair<string, string> info, bool cloud = true);

        public bool AlterLocalDBkey(string key);

        public void TemporaryRelease(bool isCloud = true);

        public bool LocalDBSurvival(string dbkey);

        public Task<SqliteExceuteResult> LocalDBQueryFree(params string[] sqls);

        public Task<SqliteExceuteResult> LocalDBQuery(List<string> sql);

        public Task<bool> CheckField<FiedType>(string table, string field, FiedType value);

        public void CloudAnchorChannel<Table>(Action execute, string? type = null);

        public void Dispose();
    }

    public interface IDirFileServer
    {
        public delegate void ReceiveLine(string conent);

        public string GetSuffix(string file);

        public Task ReadAsync(string file, ReceiveLine receiveLine);

        public Task<List<string>> ReadAsync(string file);

        public List<string> Read(string file);

        public Task<List<string>> GetLocalFilesAsync(
            List<string> paths, 
            List<string> keyworks, 
            List<string> extensions,
            Action<string> realtimerdata,
            sbyte mode = 0);

        public Task<List<string>> GetLocalFilesAsync(
            string path,
            List<string> keyworks,
            List<string> extensions,
            Action<string> realtimerdata,
            sbyte mode = 0);

        public void GetLocalFilesAsyncTovoid(
            string path,
            List<string> keyworks,
            List<string> extensions,
            Action<string> realtimerdata,
            sbyte mode = 0);

        public Task<Dictionary<string, Dictionary<string, List<string>>>> GetExcel(ExcelOptions options, ExcelOriented orient = ExcelOriented.ColumnOriented);

        public void GetLocalFilesCancelletion(bool isDispose);

        public string GetSimpleName(string file);

        public bool CreateDir(string dir);

        public bool CreateFile(string file);

        public bool IsFile(string file);

        public bool IsDir(string dir);

        public string GetDir(string file);

        public string RegistryAction(int type, string path, string key, string value="");

        public string RegistryAction(int type, string path, string name, string key, string value="");

        public string RegistryAction(int type, RegisterPath path, string name, string key, string value="");
    }

    public interface IDenCryptoGraphic
    {
        public string EnCryptoGraphic(string content, string password);

        public string DeCryptoGraphic(string crypt,   string password);

        public string ENbase64(string enstring);

        public string DEbase64(string destring);
    }


    public interface IActivityDetetorAssist
    {
        public Task<object?> AutoFindDim(string content, string flag);


        public Task<Supabase.Postgrest.Responses.ModeledResponse<WorkOrderUnfold>?> WorkOrder_CloudDimMatch(string content, string flag);


        public Task<Dictionary<string, List<object>>?> WorkOrder_LocalDimMatch(string content, string flag);
    }
}
